package com.cn.lotary.util;

import org.apache.log4j.Logger;

/**
 *
 * @author Jason Jun 30, 2011
 */
public class LoggerUtils {

    private static final Logger log = Logger.getLogger("");

    public LoggerUtils() {
    }

    public static void error(Exception e, String message) {
        log.error(formatMessage(e, message));
    }

    public static void error(Exception e) {
        log.error(formatMessage(e));
    }

    /**
     * only useful test
     *
     * @param e
     */
    public static void out(Object e) {
        if (SystemConfig.DEBUG) {
            System.err.println("[debug test output: ]" + e);
        }
    }

    public static void debug(Exception e) {
        if (SystemConfig.DEBUG) {
            log.debug(formatMessage(e));
        }
    }

    public static void debug(String e) {
        if (SystemConfig.DEBUG) {
            log.debug(e);
        }
    }

    public static void warn(Exception e) {
        log.warn(formatMessage(e));
    }

    public static void warn(Exception e, String message) {
        log.warn(formatMessage(e, message));
    }

    public static void warn(String message) {
        log.warn(message);
    }

    public static void info(Exception e) {
        log.info(formatMessage(e));
    }

    public static void info(Object e) {
        log.info(e);
    }

    public static void info(Class<?> clas, Object message) {
        log.info("[" + clas + "] \r\n\t" + message);
    }

    public static void warn(Class<?> clas, String message) {
        log.warn("[" + clas + "] \r\n\t" + message);
    }

    private static String formatMessage(Exception e) {
        String className = e.getStackTrace()[0].getClassName();
        String methodName = e.getStackTrace()[0].getMethodName();
        int lineNumber = e.getStackTrace()[0].getLineNumber();
        String localizedMessage = e.getLocalizedMessage();
        return '[' + className + "][" + methodName + "][" + lineNumber + "]\r\n\t[" + localizedMessage + ']';
    }

    private static String formatMessage(Exception e, String message) {
        return formatMessage(e) + '[' + message + ']';
    }
}
